package com.photoeditor.demo.ad;

import android.app.Activity;
import android.text.TextUtils;

import com.common.base.ComponentContext;
import com.common.base.utils.ArrayUtils;
import com.common.base.utils.ProcessUtils;
import com.common.commercialize.channel.BuyUserManager;
import com.common.commercialize.utils.TaskRunnable;
import com.common.statistics.StatisticsUtils;
import com.photoeditor.demo.statistics.StatisticsConstant;


//import com.billing.base.svip.SVipManager;

/**
 * 编辑完成广告，暂时统一处理，包括ImageEdit,CollageEdit, VideoEdit
 * <p>
 * Created by mutour on 2017/10/28.
 */

public class EditCompleteAdProvider {

    private static final String TAG = "editcomplete";

    private final FullScreenAdProvider mAdProvider;

    public static final String SCENE_NAME = "savephoto";

    public Activity mActivity;

    public EditCompleteAdProvider(Activity activity) {
        mActivity = activity;
        mAdProvider = new FullScreenAdProvider(SCENE_NAME, AdKeyConstant.InappAdPlacementID.EDIT_SAVE_ID, -1);
    }

    public void loadAd() {
        if (mAdProvider.needRequest(0)) {
            mAdProvider.loadAd(mActivity);
        }
    }

    public boolean showAd(TaskRunnable runnable) {
        return mAdProvider.showAd(runnable, "editcomplete");
    }

    public void destroyAd() {
        mAdProvider.destroyAd();
    }

    public static void statistics(String eventSuffix, String... attributes) {
        if (ProcessUtils.isMainProcess(ComponentContext.getContext())) {
            String[] attrs = ArrayUtils.addAll(attributes, "usertype", String.valueOf(BuyUserManager.getInstance().isBuyUser()));
            StatisticsUtils.statisics(StatisticsConstant.UMENG_CUSTOM_EVENT_PREFIX_FUNCTION + "_edit", eventSuffix, attrs);
        }
    }

    public static final int TYPE_SAVE = 1;
    public static final int TYPE_EXIT = 2;

    public void showAd(int type, final TaskRunnable taskRunnable, String eventSuffix, String... attributes) {
        /*if (AppVipManager.isVip()) {
            mAdProvider.statisticsNoShowAd(AdProvider.REASON_SVIP_USER);
            if(taskRunnable != null) {
                taskRunnable.runSelfAdaptation();
            }
            destroyAd();
        } else if (type == TYPE_SAVE && System.currentTimeMillis() - AppApplication.getInstallTime() < TimeConstant.ONE_DAY * 3
                && !SVipController.getInstance().getHasShowSVipOfEditComplete()
                && SVipController.getInstance().canShowSVipActivityAutoToday()) {
//            SVipController.getInstance().showPurchaseActivity(SVipController.ENTRANCE_FIRST_EDIT_FINISHED, new SVipController.OnSVipPurchaseCloseListener() {
//                @Override
//                public void onSVipPurchaseClose() {
                    if(taskRunnable != null) {
                        taskRunnable.runSelfAdaptation();
                    }
//                }
//            },  false);
//            SVipController.getInstance().setHasShowSVipOfEditComplete(true);
//            mAdProvider.statisticsNoShowAd(AdProvider.REASON_SHOULD_SHOW_SVIP);
            destroyAd();
        } else */{
            if(!TextUtils.isEmpty(eventSuffix) && attributes != null) {
                statistics(eventSuffix, attributes);
            }
            if (!showAd(taskRunnable)) {
                if(taskRunnable != null) {
                    taskRunnable.runSelfAdaptation();
                }
            }
        }
    }

    public void onSave(TaskRunnable taskRunnable, String where) {
        showAd(TYPE_SAVE, taskRunnable, "save", "where", where);
    }

    public void onExit(TaskRunnable taskRunnable) {
        showAd(TYPE_EXIT, taskRunnable, "");
    }

}
